﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualPress.Infrastructure.Entities.Repository;
using VirtualPress.Infrastructure.Entities.Cache;

namespace VirtualPress.Infrastructure.Entities.Entity
{
    public class Book : IAccessible<B_Book, Book>, ILogicEntity<B_Book, Book>
    {
        #region
        public int Id { get; set; }
        /// <summary>
        /// 书名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 作者
        /// </summary>
        public string Authors { get; set; }
        /// <summary>
        /// 译者
        /// </summary>
        public string Translator { get; set; }
        /// <summary>
        /// 出版社
        /// </summary>
        public string Press { get; set; }
        /// <summary>
        /// 书籍ISBN号
        /// </summary>
        public string ISBN { get; set; }
        /// <summary>
        /// 发售范围
        /// </summary>
        public string FXFW { get; set; }
        /// <summary>
        /// 版次
        /// </summary>
        public string BC { get; set; }
        /// <summary>
        /// 印次
        /// </summary>
        public string YC { get; set; }
        /// <summary>
        /// 开本
        /// </summary>
        public string KB { get; set; }
        /// <summary>
        /// 印张
        /// </summary>
        public int Pages { get; set; }
        /// <summary>
        /// 字数
        /// </summary>
        public int Characters { get; set; }
        /// <summary>
        /// 承印者
        /// </summary>
        public string CYZ { get; set; }
        /// <summary>
        /// 价格
        /// </summary>
        public decimal Price { get; set; }
        /// <summary>
        /// 简介
        /// </summary>
        public string Introduction { get; set; }
        /// <summary>
        /// 购买方式
        /// </summary>
        public string Buy { get; set; }
        /// <summary>
        /// 封面
        /// </summary>
        public string CoverImg { get; set; }
        /// <summary>
        /// 出版时间
        /// </summary>
        public DateTime PubDate { get; set; }
        /// <summary>
        /// 印刷时间
        /// </summary>
        public DateTime PrintDate { get; set; }
        /// <summary>
        /// 包装
        /// </summary>
        public string Pack { get; set; }
        /// <summary>
        /// 书籍内容
        /// </summary>
        public string Content { get; set; }
        /// <summary>
        /// 作者简介
        /// </summary>
        public string AboutAuthor { get; set; }
        /// <summary>
        /// 评论
        /// </summary>
        public string Comments { get; set; }
        #endregion

        protected override void Covariant(B_Book model)
        {
            if (model != null)
            {
                this.Authors = model.Authors;
                this.BC = model.BC;
                this.Characters = model.Characters;
                this.CoverImg = model.CoverImg;
                this.CYZ = model.CYZ;
                this.FXFW = model.FXFW;
                this.Id = model.Id;
                this.Introduction = model.Introduction;
                this.ISBN = model.ISBN;
                this.KB = model.KB;
                this.Name = model.Name;
                this.Pages = model.Pages;
                this.Press = model.Press;
                this.Price = model.Price;
                this.Translator = model.Translator;
                this.Buy = model.Buy;
                this.YC = model.YC;
                this.PubDate = model.PubDate;
                this.PrintDate = model.PrintDate;
                this.Pack = model.Pack;
                this.Content = model.Content;
                this.AboutAuthor = model.AboutAuthor;
                this.Comments = model.Comments;
            }
        }

        protected override B_Book Inverter(Book model)
        {
            if (model != null)
            {
                return new B_Book()
                {
                    Authors = model.Authors,
                    BC = model.BC,
                    Characters = model.Characters,
                    CoverImg = model.CoverImg,
                    CYZ = model.CYZ,
                    FXFW = model.FXFW,
                    Id = model.Id,
                    Introduction = model.Introduction,
                    ISBN = model.ISBN,
                    KB = model.KB,
                    Name = model.Name,
                    Pages = model.Pages,
                    Press = model.Press,
                    Price = model.Price,
                    Translator = model.Translator,
                    Buy = model.Buy,
                    YC = model.YC,
                    Pack=model.Pack,
                    PrintDate=model.PrintDate,
                    PubDate=model.PubDate,
                    AboutAuthor=model.AboutAuthor,
                    Comments=model.Comments,
                    Content=model.Content
                };
            }
            return null;
        }

        public Book Set(B_Book dbModel)
        {
            Covariant(dbModel);
            return this;
        }
        public override Book Find(string key)
        {
            return new BookCache().GetItem(key);
        }
        public override Book PostDelete()
        {
            new BookCache().RefreshItem(Id.ToString());
            return base.PostDelete();
        }
        public override Book PostModify()
        {
            new BookCache().RefreshItem(Id.ToString());
            return base.PostModify();
        }
    }
}
